<?php

namespace common\behavior\CommonMessagePushing;


use common\model\GlobalNoticeModel;
use common\model\UserModel;
use common\service\BehaviorService;
use common\service\OptionsService;
use common\service\ProjectStepCategoryConfirmConfigService;
use common\service\ReviewRequestMessageService;
use Webman\Stomp\Client;

/**
 * 公告 消息通知
 */
class GlobalNoticePublishBehavior
{
    /**
     * @param $params
     * @return void
     */
    public function run($params)
    {
        /***********  预处理开始  ************/
        $params = BehaviorService::getInstance()->dealParam(
            $params,
            [
                'notice_id' => 'intval',
                'notice_data' => '',
            ],
            [
                'notice_id' => 0,
                'notice_data' => [],
            ],
            'data',
            ['type' => 'global_notice_publish'],
        );
        /***********  行为判断  ************/
        if ($params === false) {
            return;
        }
        /***********  变量迁移  ************/
        list($noticeId, $noticeData) = $params;
        if (empty($noticeId) || empty($noticeData)) {
            return;
        }
        /***********  预处理结束  ************/

        /***********  消息内容组织  ************/
        $messageGenerated = [];
        switch (intval($noticeData['type'])) {
            case GlobalNoticeModel::GLOBAL_NOTICE_TYPE_QUALITY_CONTROL:
                $title = '质检公告';
                $noticeType = 'message_producer';
                $messageKey = 'global_notice_quality_control';
                break;
            case GlobalNoticeModel::GLOBAL_NOTICE_TYPE_QA:
                $title = 'QA公告';
                $noticeType = 'message_qa';
                $messageKey = 'global_notice_quality_assurance';
                break;
            default:
                return;
        }
        $noticeTitle = $noticeData['title'] ?? [];
        $receiverIdList = $noticeData['join_users'] ?? [];
        $userMap = model(UserModel::class)->where(['id' => ['in', $receiverIdList]])->field('id,name,union_id')->select();
        $messageParam = [
            'param' => [
                'first' => $title,
                'note_text' => $noticeTitle,
                'link_id' => $noticeId,
                'link_module_code' => 'global_notice',
                'operate_at' => time(),
                'created_by' => $noticeData['created_by'] ?? 0,
            ],
            'template_id' => OptionsService::getInstance()->getTaskRemindCommonTemplateId(),
            'content_type' => empty($noticeData['global_notice_receive_way'] ?? '') ? 'common_popover' : 'common_modal',
            'is_popup' => 1,
            'message_key' => $messageKey,
        ];
        $userIdMap = array_column($userMap, null, 'id');
        foreach ($receiverIdList as $user) {
            if (!isset($userIdMap[$user])) {
                continue;
            }
            $messageGenerated[] = array_replace($messageParam, [
                'receiver' => $userIdMap[$user]['union_id'],
                'notice_type' => $noticeType
            ]);
        }
        /***********  发送新的队列  ************/
        foreach ($messageGenerated as $message) {
            var_dump($message);
            Client::send('common_message_pushing', build_queue_data_with_xu([
                'message_generated' => [$message],
            ]));
        }
        /***********  结束  ************/
    }
}
